package com.jianyun.jyzs.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.jianyun.jyzs.utils.JoinImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mlli on 2017/11/17.
 */

public class JointImage extends ImageView{
    private List<Bitmap> list = new ArrayList<>();
    private Paint paint;
    private Rect mSrcRect, mDestRect;
    private Matrix matrix;

    public JointImage(Context context) {
        super(context);
        init();
    }



    public JointImage(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public JointImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);//如果该项设置为true，则图像在动画进行中会滤掉对Bitmap图像的优化操作，加快显示速度
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setColor(0xaa0096ec);
        matrix = new Matrix();

    }
    public void setImageList(List<Bitmap> list){
        this.list = list;
        mSrcRect = new Rect(0, 0, width, height);
        invalidate();

    }

    public void setImageLista(List<String> lists){
        for (int i = 0; i <lists.size() ; i++) {
           final String s = lists.get(i);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Bitmap getbitmapa= JoinImageUtils.getbitmap(s);
                    list.add(getbitmapa);
                }
            }).start();

        }
        mSrcRect = new Rect(0, 0, width, height);
        invalidate();
//        this.list = list;
//        mSrcRect = new Rect(0, 0, width, height);
//        invalidate();



    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (list.size()>0)drawImages(canvas);
    }

    private void drawImages(Canvas canvas) {
        ArrayList<Bitmap> scaleBitmap = scaleBitmap();
        if (scaleBitmap.size()==1){
            mDestRect = new Rect(0, 0, width , height );
            canvas.drawBitmap(scaleBitmap.get(0), mSrcRect, mDestRect, paint);
        }else {
            for (int i = 0; i < scaleBitmap.size(); i++) {
                if (i == 0) {
                    mDestRect = new Rect(0, 0, width / 2, height / 2);
                    canvas.drawBitmap(scaleBitmap.get(i), mSrcRect, mDestRect, paint);
                } else if (i == 1) {
                    mDestRect = new Rect(width / 2, 0, width, height / 2);
                    canvas.drawBitmap(scaleBitmap.get(i), mSrcRect, mDestRect, paint);
                } else if (i == 2) {
                    mDestRect = new Rect(0, height / 2, width / 2, height);
                    canvas.drawBitmap(scaleBitmap.get(i), mSrcRect, mDestRect, paint);
                } else if (i == 3) {
                    mDestRect = new Rect(width / 2, height / 2, width, height);
                    canvas.drawBitmap(scaleBitmap.get(i), mSrcRect, mDestRect, paint);
                }
            }
        }
    }

    //根据比例缩放bitmap
    private ArrayList<Bitmap> scaleBitmap() {
        ArrayList<Bitmap> list = new ArrayList<>();
        for (Bitmap bit: this.list) {
            Matrix matrix = new Matrix();
            // 缩放原图
            matrix.postScale(0.3f, 0.3f);
            Bitmap dstbmp = Bitmap.createBitmap(bit, 0, 0, bit.getWidth(), bit.getHeight(),
                    matrix, true);
            list.add(dstbmp);
        }
        return list;


    }

    private int width = 0;
    private int height = 0;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.width = w;
        this.height = h;
    }

    private String url = "http://211.159.178.176:8082/bcf/webFiles/yuntechoa/photo/201711/15/171115184913000.jpg";


}
